package com.nf.health.app.datamain.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.utils.DateUtil;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.ViewUtil;

import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepann on 2016/3/29.
 * The only genius that is worth anything is the genius for hard work
 *
 * @author hepann
 * @Description 血压测量结果展示类
 * @emial he_pann@163.com
 */
public class BloodPresureMeasuredResultActivity extends BaseActivity {

    @Bind(R.id.iv_measured_result_bk)
    ImageView ivMeasuredResultBk;
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_high_presure)
    TextView tvHighPresure;
    @Bind(R.id.tv_low_presure)
    TextView tvLowPresure;
    @Bind(R.id.tv_heart_rate)
    TextView tvHeartRate;
    @Bind(R.id.btn_measure)
    Button btnMeasure;

    private int images[] = {
            R.drawable.measure_result_xyj1,
            R.drawable.measure_result_xyj2,
            R.drawable.measure_result_xyj3,
            R.drawable.measure_result_xyj4,
            R.drawable.measure_result_xyj5,
            R.drawable.measure_result_xyj6
    };
    /**
     * 高压,低压,心率
     */
    private String[] detailData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleBar(R.layout.titlebar_base);
        setContent(R.layout.activity_blood_pressure_measured_result);
        ButterKnife.bind(this);

        ViewUtil.setText(this, R.id.titlebar_title_tv, "测量结果");
        btnMeasure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //获得sp存储的血压信息(高压,低压,心率)
        String bloodPresure = PreferenceHelper.getString(PreferenceHelper.BLOOD_PRESSURE_MEASURED_INFO, "");
        this.detailData = bloodPresure.split(",");

        initData(detailData);

        //根据高低压判定健康等级
        int level = levelJudgmentByPressure();
        ivMeasuredResultBk.setBackgroundResource(images[level]);
        tvDate.setText(DateUtil.date2HHmm(new Date()));
    }

    /**
     * 设置测量得到的数据
     *
     * @param detailData sp得到的数据分拆成数组
     */
    private void initData(String[] detailData) {
        if (detailData.length == 3) {
            tvHighPresure.setText(detailData[0]);
            tvLowPresure.setText(detailData[1]);
            tvHeartRate.setText(detailData[2]);
        }
    }

    /**
     * 根据高低血压返回等级 0-5
     *
     * @return 健康等级
     */
    private int levelJudgmentByPressure() {
        int highLevel = 0;
        int lowLevel = 0;
        if (detailData.length == 3) {//三个数据都有才进行判断
            int highPressure = Integer.valueOf(detailData[0]);
            int lowPressure = Integer.valueOf(detailData[1]);
            if (highPressure < 100) {
                highLevel = 0;
            } else if (highPressure <= 120) {
                highLevel = 1;
            } else if (highPressure <= 130) {
                highLevel = 2;
            } else if (highPressure <= 159) {
                highLevel = 3;
            } else if (highPressure <= 179) {
                highLevel = 4;
            } else if (highPressure >= 180) {
                highLevel = 5;
            }

            if (lowPressure < 60) {
                lowLevel = 0;
            } else if (lowPressure <= 80) {
                lowLevel = 1;
            } else if (lowPressure <= 85) {
                lowLevel = 2;
            } else if (lowPressure <= 99) {
                lowLevel = 3;
            } else if (lowPressure <= 109) {
                lowLevel = 4;
            } else if (lowPressure >= 110) {
                lowLevel = 5;
            }
        }
        //高低压分别判断取最大等级
        return Math.max(highLevel, lowLevel);
    }
}
